/**
 * Transform log data into test session format
 * @param {Array} logs - Array of log entries
 * @returns {Array} Array of test sessions with start and end times
 */
export function transformLogData(logs) {
    // Sort logs by timestamp first
    const sessions = new Map();
    const pendingEndSessions = new Map();
    
    logs.forEach(log => {
        const key = `${log.TesterRunner}_${log.AgentName}`;
        
        if (log.Message.includes('Start SubTestProcess')) {
            sessions.set(key, {
                testRunner: log.TesterRunner,
                agentName: log.AgentName,
                startTime: log['@t'],
                endTime: pendingEndSessions.has(key) ? pendingEndSessions.get(key) : null
            });
            pendingEndSessions.delete(key);
        } else if (log.Message.includes('End SubTestProcess')) {
            if (sessions.has(key)) {
                sessions.get(key).endTime = log['@t'];
            } else {
                pendingEndSessions.set(key, log['@t']);
            }
        }
    });

    return Array.from(sessions.values());
}


/**
 * 从一个 Date 对象中减去指定的小时数
 * @param {Date} date - 要操作的 Date 对象
 * @param {number} hours - 要减去的小时数
 * @returns {Date} - 减去指定小时数后的新 Date 对象
 */
export function subtractHours(date, hours) {
    let millisecondsToSubtract = hours * 60 * 60 * 1000; // 计算要减去的毫秒数
    let newTime = date.getTime() - millisecondsToSubtract; // 减去指定小时数的毫秒数
    return new Date(newTime); // 返回新的 Date 对象
}
